package com.example.medicalapp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.medicalapp.R;
import com.example.medicalapp.adapter.PatientListAdapter;
import com.example.medicalapp.helper.DatabaseHelper;
import com.example.medicalapp.model.Appointment;

import java.util.List;

public class DoctorDepartmentActivity extends AppCompatActivity {
    private static final String TAG = "DoctorDepartmentActivity";
    public static final String EXTRA_APPOINTMENT_ID = "com.example.medicalapp.EXTRA_APPOINTMENT_ID";
    public static final String EXTRA_PATIENT_ID = "com.example.medicalapp.EXTRA_PATIENT_ID";
    public static final String EXTRA_PATIENT_NAME = "com.example.medicalapp.EXTRA_PATIENT_NAME";

    private RecyclerView rvPatients;
    private PatientListAdapter adapter;
    private DatabaseHelper dbHelper;
    private TextView tvDeptTitle;

    private int deptId;
    private String deptName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_department);
        Log.d(TAG, "onCreate called");

        // 初始化视图
        initViews();

        // 初始化数据库帮助类
        dbHelper = new DatabaseHelper(this);

        // 获取从上一个页面传递的科室信息
        getIntentData();

        // 加载待诊患者列表
        loadPendingPatients();
    }

    private void initViews() {
        tvDeptTitle = findViewById(R.id.tv_dept_title);
        rvPatients = findViewById(R.id.rv_patients);
        rvPatients.setLayoutManager(new LinearLayoutManager(this));
        Log.d(TAG, "Views initialized");
    }

    private void getIntentData() {
        // 获取科室ID和名称
        deptId = getIntent().getIntExtra(DoctorHomeActivity.EXTRA_DEPT_ID, -1);
        deptName = getIntent().getStringExtra(DoctorHomeActivity.EXTRA_DEPT_NAME);

        if (deptId == -1 || deptName == null) {
            Log.e(TAG, "Invalid department data received");
            finish();
            return;
        }

        // 显示科室标题
        tvDeptTitle.setText(deptName + " - 待诊患者");
        Log.d(TAG, "Received department: " + deptName + " (ID: " + deptId + ")");
    }

    private void loadPendingPatients() {
        // 从数据库获取指定科室的待诊患者
        List<Appointment> appointments = dbHelper.getPendingAppointmentsByDept(deptId);
        Log.d(TAG, "Loaded " + appointments.size() + " pending appointments for dept: " + deptId);

        // 设置适配器
        adapter = new PatientListAdapter(appointments, this::onPatientClick);
        rvPatients.setAdapter(adapter);
    }

    // 患者点击事件处理
    private void onPatientClick(Appointment appointment) {
        Log.d(TAG, "Patient clicked: " + appointment.getPatientName() +
                " (Appointment ID: " + appointment.getId() + ")");

        // 跳转到患者诊疗页面，并传递必要信息
        Intent intent = new Intent(this, PatientDetailActivity.class);
        intent.putExtra(EXTRA_APPOINTMENT_ID, appointment.getId());
        intent.putExtra(EXTRA_PATIENT_ID, appointment.getPatientId());
        intent.putExtra(EXTRA_PATIENT_NAME, appointment.getPatientName());
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 重新加载数据，确保显示最新状态
        loadPendingPatients();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
        Log.d(TAG, "onDestroy called");
    }
}